package com.iuhao.web.core.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 阿里云文件服务
 * 
 * @author caiLinFeng
 * @date 2018年2月22日
 */
@Component
public class AliOssUtils {

	final String END_POINT = "oss-cn-hangzhou.aliyuncs.com";
	final String ACCESS_KEY_ID = "LTAIKHRL303f8tVO";
	final String ACCESS_KEY_SECRET = "wmQvDVWplyUE36u9la3px5U35bHx38";

	@Value("${oss.bucket.name}")
	private String bucketName;

	@Value("${oss.base.url}")
	private String baseUrl;

	public String putObject(String suffix, InputStream input) {
		OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String key = sdf.format(new Date()) + "/" + UUID.randomUUID() + "." + suffix;
		ossClient.putObject(bucketName, key, input);
		ossClient.shutdown();
		return baseUrl + key;
	}
}
